﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace MusteriGirisi
{
    public partial class Form1 : XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxEdit1.SelectedIndex = 0;
        }

        private bool CariBul(string carikod)
        {
            bool bulundu = false;
            string cnnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format("select  name from CUSTOMERS WITH (NOLOCK) WHERE     (code = '{0}')", txtKodu.Text), cn))
                {
                    object obj = cmd.ExecuteScalar();
                    if (obj != null)
                    {
                        txtAdi.Text = obj.ToString();
                        bulundu = true;
                    }
                }
                cn.Close();
            }
            return bulundu;
        }

        private void textEdit1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (comboBoxEdit1.SelectedIndex > -1)
                {
                    txtAdi.Text = "";
                    if (CariBul(txtKodu.Text))
                    {
                        UpdateCustomer();
                        comboBoxEdit1.SelectedIndex = 0;
                        txtKodu.Text = "";
                        MessageBox.Show("Giriş Yapıldı !");
                    }
                    else
                    {
                        MessageBox.Show("Cari Bulunamadı !");
                        txtKodu.Text = "";
                    }
                }
            }
            else if (e.KeyCode == Keys.F10)
            {
                if (comboBoxEdit1.SelectedIndex > -1)
                {
                    txtAdi.Text = "";
                    frmListe liste = new frmListe();
                    liste.ListeTipi = frmListe.ListeTipleri.CariAdi;
                    liste.Filtre = txtKodu.Text;
                    if (liste.ShowDialog(this) == DialogResult.OK && liste.Value.Length > 0)
                    {
                        txtKodu.Text = liste.Value[0];
                        txtAdi.Text = liste.Value[1];
                        UpdateCustomer();
                        comboBoxEdit1.SelectedIndex = 0;
                        txtKodu.Text = "";
                        MessageBox.Show("Giriş Yapıldı !");
                    }
                }
            }
        }

        private void UpdateCustomer()
        {
            string cnnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format("UPDATE    CUSTOMERS  SET logged_in = 1,logged_type={1} WHERE     (code = '{0}')", txtKodu.Text, comboBoxEdit1.SelectedIndex), cn))
                {
                    cmd.ExecuteNonQuery();
                }
                cn.Close();
            }
        }
    }
}
